use std::{fs::File, io::Read, path::PathBuf};

use criterion::{criterion_group, criterion_main, Criterion};
use develop::common_srv;

fn criterion_benchmark(c: &mut Criterion) {
    let mut root_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    root_path.pop();
    let json = load_request_body(
        &root_path,
        "ESPBillResolver", // LargetDICTService
    );

    // let start = std::time::Instant::now();
    // for _ in 0..100_000_000 {
    //     common_srv::deserizlize_request_json(json.as_str());
    // }
    // println!("time cost: {:?} ms", start.elapsed().as_millis()); // ms

    c.bench_function("iosp deserizlize json", |b| {
        b.iter(|| common_srv::deserizlize_request_json(json.as_str()))
    });
}

fn load_request_body(root_path: &PathBuf, svr_name: &str) -> String {
    let mut file_path = PathBuf::from(root_path);
    file_path.push(".iosp");
    file_path.push("restapi_example");
    file_path.push(svr_name);
    file_path.push("request.json");

    let mut file = File::open(file_path.as_path())
        .expect(format!("can not find file: {}", file_path.to_string_lossy()).as_str());
    let mut json = String::new();
    file.read_to_string(&mut json).expect("read json faield");
    json
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
